﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Mabna.Total.BOHelper;
using EShop.Common;
using EShop.BussinessObjects;
using EShop.DataAccess;
using EShop.BusinessLogic;

public partial class mpEShop : System.Web.UI.MasterPage,ICallbackEventHandler
{
    string strResult = "";
    protected void Page_Init(object sender, EventArgs e)
    {
        Page.ClientScript.RegisterClientScriptInclude("jquery", ResolveUrl("~/Scripts/jquery-1.8.2.min.js"));
        Page.ClientScript.RegisterClientScriptInclude("slider", ResolveUrl("~/Scripts/slider.min.js"));
        Page.ClientScript.RegisterClientScriptInclude("utils", ResolveUrl("~/Scripts/utils.js"));
        Page.ClientScript.RegisterClientScriptInclude("json2", ResolveUrl("~/Scripts/json2.js"));
        Page.ClientScript.RegisterClientScriptInclude("Eshop", ResolveUrl("~/Scripts/EShop.js"));
        Page.ClientScript.RegisterClientScriptInclude("jQueryUI", ResolveUrl("~/Scripts/jquery-ui-1.9.1.min.js"));
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        #region CallBack Scripts

        string cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "signinReceive", "context", true);

        string callbackScript;
        callbackScript = "function signinCallServer(arg, context)" +
            "{" + cbReference + "} ;";
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
            "signinCallServer", callbackScript, true);

        #endregion

        if (!IsPostBack)
        {
            Company _company = CGlobalData.GetCurrentCompany();
            if (_company != null)
            {
                lnkGalleries.Visible = _company.IsShowGallery;
                lnkWeeklyMenu.Visible = _company.IsShowWeeklyMenu;
                lblPhone.InnerText = _company.Telephone;
            }

            if (CGlobalData.IsLoggedIn())
            {
                hdnIsloggedIn.Value = "true";
                hdnUserInfo.Value = CGlobalData.GetCurrentLoginID();
            }
        }
    }

    #region ICallbackEventHandler Members

    public string GetCallbackResult()
    {
        return strResult;
    }

    public void RaiseCallbackEvent(string eventArgument)
    {
        List<Object> ClientData = (List<Object>)ObjectSerializer.FromJson(eventArgument, typeof(List<Object>));

        string Command = ClientData[0].ToString();
        List<Object> ServerData = new List<object>();

        if (Command == "Login")
        {
           User user = CBLUser.Login(ClientData[1].ToString(), ClientData[2].ToString());

           if (user!=null)
           {
               ServerData.Add("Login");
               ServerData.Add("Success");
               ServerData.Add(user.LoginID);
               CGlobalData.SetGlobalData(user);
               strResult = ServerData.ToJSON();
           }
           else
           {
               ServerData.Add("Login");
               ServerData.Add("Fail");
               strResult = ServerData.ToJSON();
           }
        }
        if (Command == "Logout")
        {
            CGlobalData.LogOut();
            ServerData.Add("Logout");
            strResult = ServerData.ToJSON();
        }
    }

    #endregion
}
